package com.mzp.User.user.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.mzp.User.user.dao.UserDao;
import com.mzp.User.user.pojo.User;

/**
 * @Description user 服务层
 * @author admin
 * @date 2021-04-14 23:05:12
*/
@Service
public class UserService {

	@Autowired
	private UserDao userDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<User> findAll() {
		return userDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<User> findSearch(Map whereMap, int page, int size) {
		Specification<User> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return userDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<User> findSearch(Map whereMap) {
		Specification<User> specification = createSpecification(whereMap);
		return userDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public User findById(String id) {
		return userDao.findById(id).get();
	}

	/**
	* 增加
	* @param user
	*/
	public void add(User user) {
		user.setId( idWorker.nextId()+"" );
		userDao.save(user);
	}

	/**
	* 修改
	* @param user
	*/
	public void update(User user) {
		userDao.save(user);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		userDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<User> createSpecification(Map searchMap) {

		return new Specification<User>() {

			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//username
				if (searchMap.get("username")!=null && !"".equals(searchMap.get("username"))) {
					predicateList.add(cb.like(root.get("username").as(String.class), "%"+(String)searchMap.get("username")+"%"));
				}
				//password
				if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
					predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
				}
				//QQ
				if (searchMap.get("QQ")!=null && !"".equals(searchMap.get("QQ"))) {
					predicateList.add(cb.like(root.get("QQ").as(String.class), "%"+(String)searchMap.get("QQ")+"%"));
				}
				//wechat
				if (searchMap.get("wechat")!=null && !"".equals(searchMap.get("wechat"))) {
					predicateList.add(cb.like(root.get("wechat").as(String.class), "%"+(String)searchMap.get("wechat")+"%"));
				}
				//email
				if (searchMap.get("email")!=null && !"".equals(searchMap.get("email"))) {
					predicateList.add(cb.like(root.get("email").as(String.class), "%"+(String)searchMap.get("email")+"%"));
				}
				//ordertotal
				if (searchMap.get("ordertotal")!=null && !"".equals(searchMap.get("ordertotal"))) {
					predicateList.add(cb.like(root.get("ordertotal").as(String.class), "%"+(String)searchMap.get("ordertotal")+"%"));
				}
				//age
				if (searchMap.get("age")!=null && !"".equals(searchMap.get("age"))) {
					predicateList.add(cb.like(root.get("age").as(String.class), "%"+(String)searchMap.get("age")+"%"));
				}
				//phone
				if (searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))) {
					predicateList.add(cb.like(root.get("phone").as(String.class), "%"+(String)searchMap.get("phone")+"%"));
				}
				//addr
				if (searchMap.get("addr")!=null && !"".equals(searchMap.get("addr"))) {
					predicateList.add(cb.like(root.get("addr").as(String.class), "%"+(String)searchMap.get("addr")+"%"));
				}
				//location
				if (searchMap.get("location")!=null && !"".equals(searchMap.get("location"))) {
					predicateList.add(cb.like(root.get("location").as(String.class), "%"+(String)searchMap.get("location")+"%"));
				}
				//loginIp
				if (searchMap.get("loginIp")!=null && !"".equals(searchMap.get("loginIp"))) {
					predicateList.add(cb.like(root.get("loginIp").as(String.class), "%"+(String)searchMap.get("loginIp")+"%"));
				}
				//lastlogintime
				if (searchMap.get("lastlogintime")!=null && !"".equals(searchMap.get("lastlogintime"))) {
					predicateList.add(cb.like(root.get("lastlogintime").as(String.class), "%"+(String)searchMap.get("lastlogintime")+"%"));
				}
				//createtime
				if (searchMap.get("createtime")!=null && !"".equals(searchMap.get("createtime"))) {
					predicateList.add(cb.like(root.get("createtime").as(String.class), "%"+(String)searchMap.get("createtime")+"%"));
				}
				//updatetime
				if (searchMap.get("updatetime")!=null && !"".equals(searchMap.get("updatetime"))) {
					predicateList.add(cb.like(root.get("updatetime").as(String.class), "%"+(String)searchMap.get("updatetime")+"%"));
				}
				//is_del
				if (searchMap.get("isDel")!=null && !"".equals(searchMap.get("isDel"))) {
					predicateList.add(cb.like(root.get("isDel").as(String.class), "%"+(String)searchMap.get("isDel")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
